package uk.ac.cam.cl.projectcharlie2010.game.player.physical;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

import ch.nostromo.tiffanys.game.move.MoveInput;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class FakePopUpVMR extends javax.swing.JFrame implements KeyListener,
WindowListener, FocusListener, VisualMoveRecogniser {
	private static final long serialVersionUID = 1;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JTextPane inputTextPane;
	private JTextPane displayTextPane;

	private boolean firstInputFocusDone = false;

	//NOTE: Ugly hack!
	private String lastStringTyped = null;

	private Object lock1 = new Object();
	private boolean alive = true;
	private boolean firstCall = true;

	public FakePopUpVMR() {
		super();
		
	}

	@Override
	public MoveInput seeNextMove() throws ImpossibleMoveException, AbortedException {
		
		if (firstCall) {
			initGUI();
			firstCall = false;
		}
		
		String from;
		String to;
		synchronized (lock1) {
			appendText("Type 'from' string:");
			try {
				lock1.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (!alive ) {
				throw new AbortedException();
			}
			from = lastStringTyped;
			appendText(from);
			appendText("");
			appendText("Type 'to' string:");
			try {
				lock1.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (!alive) {
				throw new AbortedException();
			}
			to = lastStringTyped;
			appendText(to);
			appendText("");

		}
		return new MoveInput(from, to);
	}

	@Override
	public void kill() {
		dispose();
		alive  = false;
		synchronized (lock1) {
			lock1.notify();
		}
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			this.addWindowListener(this);
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addContainerGap(12, 12)
					.addComponent(getJScrollPane1(), 0, 169, Short.MAX_VALUE)
					.addGap(12)
					.addComponent(getJScrollPane2(), 0, 83, Short.MAX_VALUE)
					.addContainerGap(12, 12));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addContainerGap(12, 12)
					.addGroup(thisLayout.createParallelGroup()
							.addComponent(getJScrollPane1(), GroupLayout.Alignment.LEADING, 0, 360, Short.MAX_VALUE)
							.addComponent(getJScrollPane2(), GroupLayout.Alignment.LEADING, 0, 360, Short.MAX_VALUE))
							.addContainerGap(12, 12));
			this.setTitle("Fake Visual Move Recogniser");
			pack();
			pack();
			this.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JTextPane getDisplayTextPane() {
		if(displayTextPane == null) {
			displayTextPane = new JTextPane();
			displayTextPane.setText("");
			displayTextPane.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));

			displayTextPane.setEditable(false);
		}
		return displayTextPane;
	}

	private JTextPane getInputTextPane() {
		if(inputTextPane == null) {
			inputTextPane = new JTextPane();
			inputTextPane.setText("Type here.");
			inputTextPane.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));

			inputTextPane.addKeyListener(this);
			inputTextPane.addFocusListener(this);

		}
		return inputTextPane;
	}

	private JScrollPane getJScrollPane1() {
		if(jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setViewportView(getDisplayTextPane());
		}
		return jScrollPane1;
	}

	private JScrollPane getJScrollPane2() {
		if(jScrollPane2 == null) {
			jScrollPane2 = new JScrollPane();
			jScrollPane2.setViewportView(getInputTextPane());
		}
		return jScrollPane2;
	}


	public void sendMessage(String text) {
		lastStringTyped = text;
		synchronized (lock1) {
			lock1.notify();
		}
	}

	public void appendText(String output) {
		assert(output.length() > 0);

		String current = displayTextPane.getText();
		if (current.equals("")) {
			displayTextPane.setText(output);
		} else {
			displayTextPane.setText(current +
					'\n' + output);
		}

		displayTextPane.setCaretPosition(current.length()+output.length()-1);
	}

	//Key listening

	@Override
	public void keyPressed(KeyEvent kev) {}

	@Override
	public void keyReleased(KeyEvent kev) {}
	@Override
	public void keyTyped(KeyEvent kev) {
		//TODO: Works on all OS?                }
		if (kev.getKeyChar() == '\n') {
			String current = inputTextPane.getText();
			sendMessage(current.substring(0,current.length()-1));
			inputTextPane.setText("");
		}
	}

	//Focus listening

	@Override
	public void focusGained(FocusEvent e) {
		if (!firstInputFocusDone) {
			firstInputFocusDone = true;
			inputTextPane.setText("");
		}
	}

	@Override
	public void focusLost(FocusEvent e) {}

	//Window listening

	@Override
	public void windowActivated(WindowEvent e) {
	}

	@Override
	public void windowClosed(WindowEvent e) {
	}

	@Override
	public void windowClosing(WindowEvent e) {
		dispose();
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
	}

	@Override
	public void windowIconified(WindowEvent e) {
	}
	@Override
	public void windowOpened(WindowEvent e) {                
	}

	@Override
	public boolean isCalibrated() {
		return true;
	}


}
